import { Component, OnInit } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { FormDefaultComponent } from './FormDefault.component';

@Component({
    selector: 'app-form-input-radio',
    templateUrl: './FormInputRadio.component.html',
})
export class FormInputRadioComponent extends FormDefaultComponent implements OnInit {
    constructor() {
        super();
    }

    ngOnInit() {
        const { formGroupReference, formControlId, options } = this.configuration;
        if (!options) return;
        if (this.configuration.required) {
            const abstractControl: AbstractControl = formGroupReference.get(formControlId);
            if (!abstractControl.value && options.length > 0) {
                abstractControl.patchValue(options[0].key);
            }
        }
    }
}
